import redis
from ZiMoooModule.system import get_new_port,is_port_in_use
from ZiMoooModule.redis_install import auto_install_redis
def ReadConfig():
    global config
    with open(".\\Config.json", "r", encoding="utf-8") as f:
        config = f.read()
        config = json.loads(config)


def StartRedis():
    global config
    if config["redis"]["port"] <= 0:
        config["redis"]["port"] = get_new_port()
    if not config["redis"]["servername"]:
        config["redis"]["servername"] = "Redis50141"
    if not config["redis"]["path"]:
        config["redis"]["path"] = ".\\Redis"
    if not config["redis"]["host"]:
        config["redis"]["host"] = "localhost"

    global redis_poll
    if auto_install_redis(path=config["redis"]["path"],
                          port=config["redis"]["port"],
                          servername=config["redis"]["servername"]):

        redis_poll = redis.ConnectionPool(host=config["redis"]["host"],
                                          port=config["redis"]["port"],
                                          db=0)
    else:
        redis_poll = None
        print("Redis 服务启动失败...")

import json
class MyConnection():
    def __init__(self):
        pass

    def add_connection(self, ConnID: int, bwid: int):
        global redis_poll
        _list = self.get_connection_list()
        data = {"ConnID": ConnID, "bwid": bwid}
        if not any(data == d for d in _list):
            _list.append(data)
            r = redis.Redis(connection_pool=redis_poll)
            r.set(name="connection_list", value=json.dumps(_list))
        return _list

    def del_connection(self, ConnID, bwid=None):
        global redis_poll
        _list = self.get_connection_list()
        n_list = []

        for item in _list:
            if bwid:
                if not (item["bwid"] == bwid and item["ConnID"] == ConnID):
                    n_list.append(item)
            else:
                if not (item["ConnID"] == ConnID):
                    n_list.append(item)
        r = redis.Redis(connection_pool=redis_poll)
        r.set(name="connection_list", value=json.dumps(n_list))
        return n_list

    def clear(self):
        global redis_poll
        r = redis.Redis(connection_pool=redis_poll)
        r.delete("connection_list")

    def get_connection_list(self):
        global redis_poll
        print("redis_poll",redis_poll)
        r = redis.Redis(connection_pool=redis_poll)
        print("r", r)
        connection_list = r.get("connection_list")
        print(connection_list)

        if r.exists("connection_list"):
            intercept_list = r.get("connection_list")
            intercept_list = json.loads(intercept_list)
            return intercept_list
        else:
            return []

    def getConnID(self, bwid: int):
        _list = self.get_connection_list()
        for item in _list:
            if item["bwid"] == bwid:
                return item["ConnID"]
        return 0


if __name__ == '__main__':
    ReadConfig()
    StartRedis()
    mr=MyConnection()
    mr.add_connection(ConnID=0,bwid=0)
